/**
 * Created on 2020-6-16
 * Last editor on 2020-6-16
 * @author: guixin
 */
package com.example.auv_660.service;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.example.auv_660.SplashActivity;

public class MyApplication extends Application {
    private static SplashActivity splashActivity = null;

    public static SplashActivity getMainActivity()
    {
        return splashActivity;
    }

    public static void setMainActivity(SplashActivity activity)
    {
        splashActivity = activity;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        if (isMainProcess(getApplicationContext()))
        {
            startService(new Intent(this, LocalService.class));
        }
        else
        {
            return;
        }
    }

    /**
     * 获取当前进程名
     * @param context
     * @return processName
     */
    public String getCurrentProcessName(Context context)
    {
        int pid = android.os.Process.myPid();
        String processName = "";
        ActivityManager manager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses())
        {
            if (process.pid == pid)
            {
                processName = process.processName;
            }
        }
        return processName;
    }

    /**
     * 是否为主进程
     * @param context
     * @return isMainProcess
     */
    public boolean isMainProcess(Context context)
    {
        boolean isMainProcess;
        isMainProcess = context.getApplicationContext().getPackageName().equals(getCurrentProcessName(context));
        return isMainProcess;
    }
}